//
//  CollocateFavoriteItemView.m
//  SecondPage
//
//  Created by xiong qi on 13-5-2.
//  Copyright (c) 2013年 winston. All rights reserved.
//

#import "CollocateFavoriteItemView.h"
//#import "../../ResourceLoader/ResourceLoader/JSONKit.h"
#import "CollocateFavoriteItemCell.h"


@implementation CollocateFavoriteItemView
@synthesize resourceData,keysData;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.delegate = self;
        self.dataSource = self;
        
    }
    return self;
}

-(void)RefreshByData:(NSDictionary *)data
{
    self.resourceData = [data objectForKey:@"data"];
    self.keysData = [data objectForKey:@"sort"];
    
    [self reloadData];
    self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, self.contentSize.height);
}

#pragma UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [keysData count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 30;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    
    NSString * key = [keysData objectAtIndex:section];
    UILabel * titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 180, 30)];
    titleLabel.text = key;
    titleLabel.textColor = [UIColor blackColor];
    titleLabel.textAlignment = NSTextAlignmentLeft;
    titleLabel.font = [UIFont fontWithName:@"Arial-BoldMT" size:20];
    [titleLabel setBackgroundColor:[UIColor clearColor]];
    [self addSubview:titleLabel];
    [titleLabel release];
    
    
    return [titleLabel autorelease];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSString * key = [keysData objectAtIndex:section];
    NSArray * array = [resourceData objectForKey:key];
    return [array count];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 20;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    CollocateFavoriteItemCell* cell = [tableView dequeueReusableCellWithIdentifier:@"CollocateFavoriteItemCell"];
    if(cell == nil)
    {
        cell = [[[CollocateFavoriteItemCell alloc] initWithStyle:UITableViewCellStyleDefault
                                      reuseIdentifier:@"CollocateFavoriteItemCell"] autorelease];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    }
    else
    {
    }
    
    NSString * key = [keysData objectAtIndex:indexPath.section];
    NSArray * array = [resourceData objectForKey:key];
    NSString * str =  [array objectAtIndex:indexPath.row];
    
    [cell LoadCellData:str];
    
    return cell;
    
}


-(void)dealloc
{
    self.resourceData = nil;
    self.keysData = nil;
    [super dealloc];
}

@end
